/**
 * 分页渲染器
 * @author: wubo
 * @createDate: 2010-8-12
 * @version: 1.0
 */
PageRender = function(params){
	var setParams = function(_urlParams, _params, filter){
		if(_params){
			for(var p in _params){
				var skip = false;
				for(var i in filter){
					if(filter[i] == p){
						skip = true;
						break;				
					}
				}
				if(! skip){
					_urlParams[p] = _params[p];
				}
			}
		}
	};
	var transParams = function(_params){
		var obj = {};
		if(_params){
			for(var p in _params){
				var _tmpValue = _params[p];
				if(typeof(_tmpValue) == 'string' && _tmpValue.startWith("#")){
					obj[p] = document.getElementById(_tmpValue.substring(1)).value;
				}else{
					obj[p] = _tmpValue;
				}
			}
		}
		return obj;
	}
	var mixUrl = function(_url, _params){
		if(_url.indexOf("?") != -1){
			_url += "&";
		}else{
			_url += "?";
		}
		var cont = 0;
		for(var p in _params){
			if(cont>0){_url += "&";}
			_url += p+"="+_params[p];cont++;
		}
		return encodeURI( _url );
	};
	var setTotal = function(total){
			this.currentPage = (total == 0 ? 0 : Math.floor( (pageParams.start + pageParams.limit) / pageParams.limit ));
			this.totalPages = (total ==0 ? 0 : Math.floor( total / pageParams.limit + (total % pageParams.limit == 0 ? 0 : 1) ));
	};
	var getId = function(id){
		if(id)return id;
		if(! PageRender.IdSequence){
			PageRender.IdSequence = 0;
		}
		return "PageRender_"+(PageRender.IdSequence ++);
	};
	var idstr = getId(params.id);
	window[idstr] = this;
	
	var filter = ["start", "limit", "total", "id", "beforeLoad", "afterLoad", "renderTo"];
	var pageParams = {};
	pageParams.noDataInfo = params.noDataInfo || "当前没有记录"; //当没有记录时显示的文字
	pageParams.start = params.start || 0;
	pageParams.limit = params.limit || 25;
	pageParams.total = params.total || 0;
	pageParams.ajaxParams = {};
	pageParams.ajaxParams.data = {};
	pageParams.postParams = {};
	var method = params.method || "get";
	method = method.toLowerCase();
	var beforeLoad = params.beforeLoad || function(){};
	var afterLoad = params.afterLoad || function(){};
	if(method == 'post'){
		pageParams.url = params.url;
	}
	if(method == 'ajax'){
		setParams.call(this, pageParams.ajaxParams, params, filter);
		var fn = params.complete || function(){};
		pageParams.ajaxParams.complete = function(){
			afterLoad.call(this, arguments);
			fn.call(this, arguments);
		};
	}
	setTotal.call(this, pageParams.total);
	
	var homestr = params.home || "首页";
	var endstr = params.end || "未页";
	var nextstr = params.next || "下页";
	var laststr = params.last || "上页";

	//alert("Start:"+start+"\tLimti:"+limit+"\tTotal:"+total+"\tCurrentPage:"+this.currentPage+"\tTotalPages:"+this.totalPages);
	this.toString = function(el){
		if(pageParams.total > 0){
			var str = [];
			str.push("<div class='page page-left'>");
			str.push("<span><strong>当前第"+this.currentPage+"/"+this.totalPages+"页</strong></span>");
			str.push("<span><strong>共"+pageParams.total+"条记录</strong></span>");
			str.push("<select onchange='javascript:window[\""+idstr+"\"].changeLimit(this.value);'>");
			var _page_limit_array = [10,15,25,50,100,150,200];
			for(var i=0; i<_page_limit_array.length; i++){
				var _page_limit = _page_limit_array[i];
				str.push("<option value='"+_page_limit+"'");
				if(_page_limit == pageParams.limit){
					str.push(" selected ");
				}
				str.push(">每页"+_page_limit+"条</option>");
			};
			str.push("</select>");
			str.push("</div>");
			//分页按钮
			str.push("<div class='page page-right manu'>");
			if(this.currentPage > 1){
				str.push("<a href='javascript:void(0);' onclick='javascript:window[\""+idstr+"\"].home();'>"+homestr+"</a>");
				str.push("<a href='javascript:void(0);' onclick='javascript:window[\""+idstr+"\"].last();'>"+laststr+"</a>");
			}else{
				str.push("<span class='disabled'>"+homestr+"</a></span>");
				str.push("<span class='disabled'>"+laststr+"</a></span>");
			}
			for(var p=this.currentPage - 3; p<this.currentPage; p++){
				if(p>0)str.push("<a href='javascript:void(0);' onclick='javascript:window[\""+idstr+"\"].position("+p+");'>"+p+"</a>");
			}
			str.push("<span class='current'>"+this.currentPage+"</span>");
			for(var p=this.currentPage+1; p<=this.totalPages && p<=this.currentPage+3; p++){
				str.push("<a href='javascript:void(0);' onclick='javascript:window[\""+idstr+"\"].position("+p+");'>"+p+"</a>");
			}
			if(this.currentPage < this.totalPages && this.totalPages > 1){
				str.push("<a href='javascript:void(0);' onclick='javascript:window[\""+idstr+"\"].next();'>"+nextstr+"</a>");
				str.push("<a href='javascript:void(0);' onclick='javascript:window[\""+idstr+"\"].end();'>"+endstr+"</a>");
			}else{
				str.push("<span class='disabled'>"+nextstr+"</span>");
				str.push("<span class='disabled'>"+endstr+"</span>");
			}
			str.push("<span><input id='pagenum' type='text' onkeydown='javascript:if(event.keyCode==13){window[\""+idstr+"\"].changePage();return false;}'>");
			str.push("<input type='button' onclick='javascript:window[\""+idstr+"\"].changePage();return false;' value='GO'/></span>");
			str.push("</div>");
			return str.join('');
		}
		return pageParams.noDataInfo;
	};
	this.refresh = function(){
		this.render(params.renderTo);
	};
	this.render = function(el){
		if(typeof(el) == "string"){
			document.getElementById(el).innerHTML = this.toString();
		}else{
			el.innerHTML = this.toString();
		}
		params.renderTo = el;
	};
	if(params.renderTo){
		this.render(params.renderTo);
	};
	this.total = function(total){
		if(total == undefined){
			return pageParams.total;
		}else if(total){
			pageParams.total = total;
			setTotal.call(this, total);
		}
	};
	this.setAjaxParams = function(params){
		setParams.call(this, pageParams.ajaxParams, params);
	};
	this.setAjaxData = function(data){
		setParams.call(this, pageParams.ajaxParams.data, data);
	};
	this.setPostParams = function(params){
		setParams.call(this, pageParams.postParams, params);
	};
	this.changeLimit = function(l){
		pageParams.limit = parseInt(l);
		this.position(1);
		return this;
	};
	this.next = function(){
		if(this.currentPage < this.totalPages){
			this.position(this.currentPage + 1);
		}
		return this;
	};
	this.last = function(){
		if(this.currentPage > 1){
			this.position(this.currentPage - 1);
		}
		return this;
	};
	this.home = function(){
		if(this.currentPage > 1){
			this.position(1);
		}
		return this;
	};
	this.end = function(){
		if(this.currentPage < this.totalPages){
			this.position(this.totalPages);
		}
		return this;
	};
	this.changePage = function(){
		var el = document.getElementById("pagenum");
		var page = el.value;
		if(page && /\d+/.test(page)){
			page = parseInt(page);
			if(page > this.totalPages)page = this.totalPages;
			if(page < 1) page = 1;
			this.position(page);
		}else{
			alert('无效的页码');el.value = '';
		}
	};
	this.position = function(page){
		pageParams.start = page * pageParams.limit - pageParams.limit;
		this.currentPage = page;
		beforeLoad(this);
		if(method == 'ajax'){
			pageParams.ajaxParams.data = transParams(pageParams.ajaxParams.data);
			pageParams.ajaxParams.data.start = pageParams.start;
			pageParams.ajaxParams.data.limit = pageParams.limit;
			if(! $ || ! $.ajax){
				throw new Error(8000, "jQuery lib is required !");
			}else{
				$.ajax(pageParams.ajaxParams);
			}
		}else{
			setParams.call(this, pageParams.postParams, {"start":pageParams.start, "limit":pageParams.limit});
			setParams.call(this, pageParams.postParams, params.data);
			if(method == 'post'){
				openPostWindow(pageParams.url, transParams(pageParams.postParams));
			}
			if(method == "get"){//GET方式提交，如有中文需要编码
				document.location.href = mixUrl(pageParams.url, transParams(pageParams.postParams));
			}
		}
	};
};